/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-26
 */
package org.jeecg.modules.flow.callback.delivery.task;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTask;
import org.jeecg.modules.business.delivery.service.IWmsDeliveryTaskService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 调拨任务关闭 回调Bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-26
 */
@RequiredArgsConstructor
@Component
public class DeliveryTaskCloseBean<T>  extends BaseCustomBeanRun<T> {

    private final IWmsDeliveryTaskService deliveryTaskService;

    /**
     * 业务逻辑 调拨任务关闭
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {

        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
        WmsDeliveryTask deliveryTask = deliveryTaskService.getById(taskId);
        if (Objects.isNull(deliveryTask)) {
            throw new JeecgBootException("调拨任务不存在!");
        }
        if (deliveryTask.getDocumentStatus().equals(DocumentStatusEnum.COMPLETE.getCode())) {
            throw new JeecgBootException("该任务已经关闭!");
        }
        deliveryTaskService.update(new LambdaUpdateWrapper<WmsDeliveryTask>()
                .set(WmsDeliveryTask::getDocumentStatus,DocumentStatusEnum.CLOSE.getCode())
                .eq(WmsDeliveryTask::getId,taskId)
        );
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
    }
}
